package com.androidified.personalshopper.client;

import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.androidified.personalshopper.Constants;
import com.androidified.personalshopper.bean.CommerceResponse;
import com.androidified.personalshopper.bean.CommercesResponse;

public class CommerceService {

	public static CommercesResponse searchNear(String api_key, String token, double lat, double lng, int radius) {
		String url = Constants.BASE_URL + Constants.PATH + "/{api_key}/{token}/operations/commerce/search/near?lat={lat}&lng={lng}&radius={radius}";
		RestTemplate restTemplate = new RestTemplate();

		// Add the String message converter
		restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());

		// Make the HTTP GET request, marshaling the response to a String
		return restTemplate.getForObject(url, CommercesResponse.class, api_key, token, lat, lng, radius);
	}
	
	public static CommerceResponse get(String api_key, String token, String id) {
		String url = Constants.BASE_URL + Constants.PATH + "/{api_key}/{token}/operations/commerce/{id}";
		
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();

		// Add the String message converter
		restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());

		// Make the HTTP GET request, marshaling the response to a String
		CommerceResponse cr = restTemplate.getForObject(url, CommerceResponse.class, api_key, token, id);
		cr.getData().setId(id);
		return cr;
	}
}
